package o;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.DrawFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import o.dky.a;
import o.dky.c;

public abstract class ern {
    private static int ˊ = 40;

    public static Bitmap ˏ(Context context, Bitmap bitmap, boolean z) throws OutOfMemoryError {
        int i;
        int i2;
        int dimensionPixelSize = context.getResources().getDimensionPixelSize(c.ˊ);
        int dimensionPixelSize2 = context.getResources().getDimensionPixelSize(c.ͺ);
        int i3 = 0;
        if (z) {
            i3 = context.getResources().getDimensionPixelSize(c.ˏ);
        }
        DrawFilter paintFlagsDrawFilter = new PaintFlagsDrawFilter(0, 3);
        Bitmap createBitmap = Bitmap.createBitmap(dimensionPixelSize, dimensionPixelSize, Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.setDrawFilter(paintFlagsDrawFilter);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(-1);
        Rect ॱ = ॱ(bitmap);
        int i4 = ॱ.right - ॱ.left;
        int i5 = ॱ.bottom - ॱ.top;
        if (i4 > i5) {
            i = -ॱ.left;
            i2 = (-ॱ.top) + ((i4 - i5) / 2);
        } else {
            i2 = -ॱ.top;
            i = (-ॱ.left) + ((i5 - i4) / 2);
            i4 = i5;
        }
        i5 = dimensionPixelSize - (i3 * 2);
        float f = (((float) i5) * 1.0f) / (((float) i4) * 1.0f);
        Bitmap createBitmap2 = Bitmap.createBitmap(i5, i5, Config.ARGB_8888);
        Canvas canvas2 = new Canvas(createBitmap2);
        canvas2.setDrawFilter(paintFlagsDrawFilter);
        canvas2.drawARGB(0, 0, 0, 0);
        Matrix matrix = new Matrix();
        matrix.postTranslate((float) i, (float) i2);
        matrix.postScale(f, f);
        canvas2.drawBitmap(bitmap, matrix, paint);
        canvas.drawRoundRect(new RectF((float) i3, (float) i3, (float) (dimensionPixelSize - i3), (float) (dimensionPixelSize - i3)), (float) dimensionPixelSize2, (float) dimensionPixelSize2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(createBitmap2, (float) i3, (float) i3, paint);
        if (z) {
            Drawable drawable = context.getResources().getDrawable(a.ͺॱ);
            drawable.setBounds(0, 0, dimensionPixelSize, dimensionPixelSize);
            drawable.draw(canvas);
        }
        return createBitmap;
    }

    public static Bitmap ॱ(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
        try {
            Bitmap createBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(createBitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return createBitmap;
        } catch (RuntimeException e) {
            Logger.ˋ("RoundIconUtil", "RuntimeException:" + e.toString());
            return null;
        } catch (Exception e2) {
            Logger.ˋ("RoundIconUtil", "Exception:" + e2.toString());
            return null;
        }
    }

    private static Rect ॱ(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] iArr = new int[(width * height)];
        bitmap.getPixels(iArr, 0, width, 0, 0, width, height);
        int ˊ = ˊ(iArr, width, height);
        int ˋ = ˋ(iArr, width, height);
        int ˏ = ˏ(iArr, width, height);
        int ॱ = ॱ(iArr, width, height);
        if (ˏ >= ॱ || ˊ >= ˋ) {
            return new Rect(0, 0, width, height);
        }
        return new Rect(ˏ, ˊ, ॱ, ˋ);
    }

    private static int ˊ(int[] iArr, int i, int i2) {
        int i3 = i % 2 == 0 ? (i / 2) - 1 : i / 2;
        for (int i4 = 0; i4 < i2; i4++) {
            int i5 = i4 * i;
            int i6 = i3;
            while (i6 >= 0) {
                if (ॱ(iArr[i5 + i6]) || ॱ(iArr[((i - 1) - i6) + i5])) {
                    return i4;
                }
                i6--;
            }
        }
        return i2;
    }

    private static int ˋ(int[] iArr, int i, int i2) {
        int i3 = i % 2 == 0 ? (i / 2) - 1 : i / 2;
        for (int i4 = i2 - 1; i4 >= 0; i4--) {
            int i5 = i4 * i;
            int i6 = i3;
            while (i6 >= 0) {
                if (ॱ(iArr[i5 + i6]) || ॱ(iArr[((i - 1) - i6) + i5])) {
                    return i4;
                }
                i6--;
            }
        }
        return 0;
    }

    private static int ˏ(int[] iArr, int i, int i2) {
        int i3 = i2 % 2 == 0 ? (i2 / 2) - 1 : i2 / 2;
        int i4 = 0;
        while (i4 < i) {
            int i5 = i3;
            while (i5 >= 0) {
                if (ॱ(iArr[(i5 * i) + i4]) || ॱ(iArr[(((i2 - 1) - i5) * i) + i4])) {
                    return i4;
                }
                i5--;
            }
            i4++;
        }
        return i;
    }

    private static int ॱ(int[] iArr, int i, int i2) {
        int i3 = i2 % 2 == 0 ? (i2 / 2) - 1 : i2 / 2;
        int i4 = i - 1;
        while (i4 >= 0) {
            int i5 = i3;
            while (i5 >= 0) {
                if (ॱ(iArr[(i5 * i) + i4]) || ॱ(iArr[(((i2 - 1) - i5) * i) + i4])) {
                    return i4;
                }
                i5--;
            }
            i4--;
        }
        return 0;
    }

    private static boolean ॱ(int i) {
        return (i >>> 24) >= ˊ;
    }
}
